package net.gyapp.markim.core;

import com.easyder.wrapper.utils.LogUtils;

import net.gyapp.markim.core.reponse.ChatResponse;
import net.gyapp.markim.core.reponse.DefaultResponse;
import net.gyapp.markim.core.reponse.HeartbeatResponse;
import net.gyapp.markim.core.reponse.ImResponse;
import net.gyapp.markim.core.reponse.JoinGroupResponse;
import net.gyapp.markim.core.reponse.LoginResponse;
import net.gyapp.markim.core.reponse.OfflineMessageResponse;
import net.gyapp.markim.core.reponse.UserGroupResponse;

import org.jim.common.base.ImPacket;

import java.io.UnsupportedEncodingException;

/**
 * Author:  winds
 * Data:    2018/7/17
 * Version: 1.0
 * Desc:    packet解析
 */


public class PacketHelper {
    

    public static ImResponse parsePacket(ImPacket packet) {
        ImResponse response = null;
        switch (packet.getCommand()) {
            case COMMAND_UNKNOW:

                break;
            case COMMAND_HANDSHAKE_REQ: // 1 握手请求，含http的websocket握手请求

                break;
            case COMMAND_HANDSHAKE_RESP:    // 2 握手响应，含http的websocket握手响应

                break;
            case COMMAND_AUTH_REQ:  // 3 鉴权请求

                break;
            case COMMAND_AUTH_RESP: // 4 鉴权响应

                break;
            case COMMAND_LOGIN_REQ: // 5 登录请求

                break;
            case COMMAND_LOGIN_RESP: // 6 登录响应
                response = ResponseParser.parse(packet, LoginResponse.class);

                break;
            case COMMAND_JOIN_GROUP_REQ:    // 7 申请进入群组

                break;
            case COMMAND_JOIN_GROUP_RESP:   // 8 申请进入群组响应

                break;
            case COMMAND_JOIN_GROUP_NOTIFY_RESP:    // 9 进入群组通知
                response = ResponseParser.parse(packet, JoinGroupResponse.class);
                break;
            case COMMAND_EXIT_GROUP_NOTIFY_RESP:    // 10 退出群组通知

                break;
            case COMMAND_CHAT_REQ:      // 11 聊天请求
                response = ResponseParser.parse(packet, ChatResponse.class);
                break;
            case COMMAND_CHAT_RESP: // 12 聊天响应
                ChatResponse parse = ResponseParser.parse(packet, ChatResponse.class);
                if(parse.data != null && parse.code == 10000) {
                    //正常返回的聊天回应信息  包含信息id   此时需要更新发出的聊天信息id
                    parse.data.cmd = 12; //用于标志当前消息需要更新
                    response = parse;
                }
                break;
            case COMMAND_HEARTBEAT_REQ: // 13 心跳请求
                response = ResponseParser.parse(packet, HeartbeatResponse.class);
                break;
            case COMMAND_CLOSE_REQ: // 14 关闭请求

                break;
            case COMMAND_CANCEL_MSG_REQ: // 15 发出撤消消息指令(管理员可以撤消所有人的消息，自己可以撤消自己的消息)
                ChatResponse recallResponse = ResponseParser.parse(packet, ChatResponse.class);
                if(recallResponse.data != null) {
                    //撤回消息命令
                    recallResponse.data.cmd = 15; //用于标志当前消息需要撤回
                    response = recallResponse;
                }
                break;
            case COMMAND_CANCEL_MSG_RESP: // 16 收到撤消消息指令   //撤回回调
//                ChatResponse recallResponseSelf = ResponseParser.parse(packet, ChatResponse.class);
//                if(recallResponseSelf.data != null) {
//                    //撤回消息命令
//                    recallResponseSelf.data.cmd = 15; //用于标志当前消息需要撤回
//                    response = recallResponseSelf;
//                }
                break;
            case COMMAND_GET_USER_REQ: // 17 获取用户信息

                break;
            case COMMAND_GET_USER_RESP: // 18 获取用户信息响应
                response = ResponseParser.parse(packet, UserGroupResponse.class);
                break;
            case COMMAND_GET_MESSAGE_REQ: // 19 获取聊天消息

                break;
            case COMMAND_GET_MESSAGE_RESP: // 20 获取聊天消息响应
                response = ResponseParser.parse(packet, OfflineMessageResponse.class);
                break;
        }

        try {
            LogUtils.info("-- Response --", new String(packet.getBody(), "utf-8").toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return response == null ? new DefaultResponse() : response;
    }
}
